package com.soulkun.smarthome.ui.main.scene

import androidx.databinding.ObservableArrayList
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainSceneAutoBinding
import com.soulkun.smarthome.databinding.AppFragmentMainSceneOnekeyBinding
import com.soulkun.smarthome.logic.model.AutoData
import com.soulkun.smarthome.logic.model.OnekeyData
import com.soulkun.smarthome.ui.main.scene.adapter.AppMainSceneAutoListAdapter
import com.soulkun.smarthome.ui.main.scene.adapter.AppMainSceneOnekeyListAdapter
import com.soulkun.smarthome.ui.main.scene.viewmodel.AppMainSceneAutoViewModel
import com.soulkun.smarthome.ui.main.scene.viewmodel.AppMainSceneOnekeyViewModel
import com.soulkun.smarthome.ui.main.scene.viewmodel.AppMainSceneViewModel
import soulkun.library.helper.decoration.GridSpaceItemDecorationHelper
import soulkun.library.helper.decoration.LinearSpaceItemDecorationHelper
import soulkun.library.mvvm.adapter.clearAndAddAll
import soulkun.library.mvvm.component.BaseDataBindingFragment

class AppMainSceneOnekeyFragment : BaseDataBindingFragment<AppFragmentMainSceneOnekeyBinding>() {

    private val viewModel by lazy {
        ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory(mActivity.application)
        ).get(AppMainSceneOnekeyViewModel::class.java)
    }

    private val adapter by lazy {
        AppMainSceneOnekeyListAdapter(mContext, onekeyList)
    }

    private val onekeyList = ObservableArrayList<OnekeyData>()

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_scene_onekey
    }

    override fun initView() {
        requireDataBinding().rvMainSceneOnekeyListOnekey.layoutManager =
            object : GridLayoutManager(mContext, 2) {
            }
        requireDataBinding().rvMainSceneOnekeyListOnekey.adapter = adapter
        requireDataBinding().rvMainSceneOnekeyListOnekey.addItemDecoration(
            GridSpaceItemDecorationHelper(15, 15)
        )
    }

    override fun initData() {
    }

    override fun initListener() {
        viewModel.onekeyListLiveData.observe(viewLifecycleOwner) {
            onekeyList.clearAndAddAll(it)
        }
    }

    override fun doOnCreate() {
    }
}